#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

FILE *ledrun_fp;
FILE *ledalarm_fp;
FILE *ledcom_fp;
FILE *ledlink_fp;
FILE *ledwifi_fp;
FILE *ledsyssta_fp;

char file_name[256];

#define ledrun_on    "0"
#define ledrun_off   "1"

#define ledalarm_on  "0"
#define ledalarm_off "1"

#define ledcom_on    "0"
#define ledcom_off   "1"

#define ledlink_on   "0"
#define ledlink_off  "1"

#define ledwifi_on   "0"
#define ledwifi_off  "1"

#define ledsyssta_on  "0"
#define ledsyssta_off "1"

void show_help(void)
{
    printf ("使用说明:\n");
#if 1
	printf ("1\t 打开运行指示灯ledrun\n");
	printf ("2\t 关闭运行指示灯ledrun\n");
	printf ("3\t 打开报警指示灯ledalarm\n");
	printf ("4\t 关闭报警指示灯ledalarm\n");
	printf ("5\t 打开连接指示灯ledlink\n");
	printf ("6\t 关闭连接指示灯ledlink\n");
	printf ("7\t 打开WIFI指示灯ledwifi\n");
	printf ("8\t 关闭WIFI指示灯ledwifi\n");
	printf ("9\t 打开系统状态指示灯ledsyssta\n");
	printf ("10\t 关闭系统状态指示灯ledsyssta\n");
#else
	printf ("1\t 打开运行指示灯ledrun\n");
	printf ("2\t 关闭运行指示灯ledrun\n");
	printf ("3\t 打开报警指示灯ledalarm\n");
	printf ("4\t 关闭报警指示灯ledalarm\n");
	printf ("5\t 打开通信指示灯ledcom\n");
	printf ("6\t 关闭通信指示灯ledcom\n");
#endif
}

int ctrl_ledrun(int onoff)
{
	char *usrAL="ual";
	char *runledN="ledrun";

	memset(file_name,0,256);

	sprintf(file_name,"/%s/%s/value",usrAL,runledN);

	if ((ledrun_fp = fopen(file_name, "rb+")) == NULL)
	{
		printf("Cannot open %s.\n",file_name);
		exit(1);
	}

	if(onoff != atoi(ledrun_off))
	{
		fprintf(ledrun_fp, ledrun_on);
	}else
	{
		fprintf(ledrun_fp, ledrun_off);
	}
	fclose(ledrun_fp);

	return 0;
}

int ctrl_ledalarm(int onoff)
{
	char *usrAL="ual";
	char *alarmledN="ledalarm";

	memset(file_name,0,256);

	sprintf(file_name,"/%s/%s/value",usrAL,alarmledN);

	if ((ledalarm_fp = fopen(file_name, "rb+")) == NULL)
	{
		printf("Cannot open %s.\n",file_name);
		exit(1);
	}

	if(onoff != atoi(ledalarm_off))
	{
		fprintf(ledalarm_fp, ledalarm_on);
	}else
	{
		fprintf(ledalarm_fp, ledalarm_off);
	}
	fclose(ledalarm_fp);

	return 0;
}

int ctrl_ledcom(int onoff)
{
	char *usrAL="ual";
	char *comledN="ledcom";

	memset(file_name,0,256);

	sprintf(file_name,"/%s/%s/value",usrAL,comledN);

	if ((ledcom_fp = fopen(file_name, "rb+")) == NULL)
	{
		printf("Cannot open %s.\n",file_name);
		exit(1);
	}

	if(onoff != atoi(ledcom_off))
	{
		fprintf(ledcom_fp, ledcom_on);
	}else
	{
		fprintf(ledcom_fp, ledcom_off);
	}
	fclose(ledcom_fp);

	return 0;
}

int ctrl_ledlink(int onoff)
{
	char *usrAL="ual";
	char *comledN="ledlink";

	memset(file_name,0,256);

	sprintf(file_name,"/%s/%s/value",usrAL,comledN);

	if ((ledlink_fp = fopen(file_name, "rb+")) == NULL)
	{
		printf("Cannot open %s.\n",file_name);
		exit(1);
	}

	if(onoff != atoi(ledlink_off))
	{
		fprintf(ledlink_fp, ledlink_on);
	}else
	{
		fprintf(ledlink_fp, ledlink_off);
	}
	fclose(ledlink_fp);

	return 0;
}

int ctrl_ledwifi(int onoff)
{
	char *usrAL="ual";
	char *comledN="ledwifi";

	memset(file_name,0,256);

	sprintf(file_name,"/%s/%s/value",usrAL,comledN);

	if ((ledwifi_fp = fopen(file_name, "rb+")) == NULL)
	{
		printf("Cannot open %s.\n",file_name);
		exit(1);
	}

	if(onoff != atoi(ledwifi_off))
	{
		fprintf(ledwifi_fp, ledwifi_on);
	}else
	{
		fprintf(ledwifi_fp, ledwifi_off);
	}
	fclose(ledwifi_fp);

	return 0;
}

int ctrl_ledsyssta(int onoff)
{
	char *usrAL="ual";
	char *comledN="ledsyssta";

	memset(file_name,0,256);

	sprintf(file_name,"/%s/%s/value",usrAL,comledN);

	if ((ledsyssta_fp = fopen(file_name, "rb+")) == NULL)
	{
		printf("Cannot open %s.\n",file_name);
		exit(1);
	}

	if(onoff != atoi(ledsyssta_off))
	{
		fprintf(ledsyssta_fp, ledsyssta_on);
	}else
	{
		fprintf(ledsyssta_fp, ledsyssta_off);
	}
	fclose(ledsyssta_fp);

	return 0;
}

int main(int argc, char **argv)
{
	int idx = -1;
	if(argc != 2 )
	{
		show_help();
		return -1;
	}
	
	idx = atoi(argv[1]);

	switch(idx)
	{
		case 1:
			{
				ctrl_ledrun(atoi(ledrun_on));
			}
			break;
		case 2:
			{
				ctrl_ledrun(atoi(ledrun_off));
			}
			break;
		case 3:
			{
				ctrl_ledalarm(atoi(ledalarm_on));
			}
			break;
		case 4:
			{
				ctrl_ledalarm(atoi(ledalarm_off));
			}
			break;
		case 5:
			{
				ctrl_ledlink(atoi(ledlink_on));
			}
			break;
		case 6:
			{
				ctrl_ledlink(atoi(ledlink_off));
			}
			break;
		case 7:
			{
				ctrl_ledwifi(atoi(ledwifi_on));
			}
			break;
		case 8:
			{
				ctrl_ledwifi(atoi(ledwifi_off));
			}
			break;
		case 9:
			{
				ctrl_ledsyssta(atoi(ledsyssta_on));
			}
			break;
		case 10:
			{
				ctrl_ledsyssta(atoi(ledsyssta_off));
			}
			break;
		default:
			{
        		show_help();
			}
			break;
	}
	return 0;
}
